﻿using ApplicationBase.Core.Service.Identity;
using ApplicationBase.Core.Utils;

namespace ApplicationBase.Core.Context
{
    public interface IServiceContext
    {
        long GenerateId();
        long GetUserId();
        string GetUserName();
        List<string> GetUserRoles();
    }

    public class ServiceContext : IServiceContext
    {
        private readonly IIdentityService _identityService;
        private readonly ISnowflakeWorker _snowflakeWorker;

        public ServiceContext(IIdentityService identityService, ISnowflakeWorker snowflakeWorker)
            => (_identityService, _snowflakeWorker) = (identityService, snowflakeWorker);

        public long GenerateId()
        {
            return _snowflakeWorker.NextId();
        }

        public long GetUserId()
        {
            return _identityService.GetUserId() ?? 0;
        }

        public string GetUserName()
        {
            return _identityService.GetUserName() ?? string.Empty;
        }

        public List<string> GetUserRoles()
        {
            return _identityService.GetUserRoles() ?? new List<string>();
        }
    }
}
